<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Lw_upload
{
    protected $CI;
    public function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->library("lw_string");
    }

    //验证文件格式
    function checkExt($arrExt,$allowExt){
        $a=0;
        for($i=0;$i<sizeof($arrExt);$i++){
            if(!in_array($this->CI->lw_string->getExt($arrExt[$i]), $allowExt )){
                $a=$a+1;
            }
        }
        return ($a==0)? true:false;
    }

    //上传文件
    function uploadFile($file,$path,$fileName) {
        if (! file_exists ( $path )) {
            mkdir ( $path, 0755, true );
        }
        // 判断错误信息
        $destination = $path . $fileName;
        if (is_uploaded_file ( $file['tmp_name'] )) {
            if (move_uploaded_file ( $file['tmp_name'], $destination )) {
                return true; //上传成功
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    // 将已上传的图片 压缩 上传到 七牛
    public function qiniuUploadImgByResize($path,$fileName,$pathResize,$qiniuName,$size=array('width'=>400,'height'=>400))
    {
        // 处理图片
        $result=array('success'=>false,'msg'=>'处理或上传图片失败');
        if($this->resizeImg($path,$fileName,$size,$pathResize)){
            $resizeImg = $pathResize.$fileName;
            // 压缩缩放图片成功
            if($this->qiniuUploadImg($resizeImg,$qiniuName)){
                $result=array('success'=>true,'msg'=>'上传图片成功');
            }
        }
        return $result;
    }

    public function qiniuUploadImg($imgPath,$ImageName)
    {
        $this->CI->load->helper('qiniu');
        qiniu_delete($ImageName);
        list($ret, $err) = qiniu_upload($imgPath,$ImageName);
        if ($err !== null) {
            //var_dump($err);
            echo "-----";
            //var_dump($ret);
            $data=array('success'=>0,'message'=>'服务器错误！','url'=>json_encode($err));
        } else {
            $data=array('success'=>1,'message'=>'上传图片成功！','url'=>QINIU_DOMAIN.$ret['key']);
        }
        return $data;
    }

    // 将上传的图片进行压缩重定义尺寸
    public function resizeImg($sourcePath,$imgName,$size=array('width'=>400,'height'=>400),$newPath=null,$thumb='')
    {
        $this->CI->load->library('image_lib');
        // 一定要清除要不然 下次调用不会执行
        $this->CI->image_lib->clear();
        if (! file_exists ( $newPath )) {
            mkdir ( $newPath, 0, true );
        }
        $config['image_library'] = 'gd2';
        $config['source_image'] = $sourcePath.$imgName;
        $config['create_thumb'] = TRUE; // 创建缩略图
        $config['thumb_marker'] = $thumb; // 文件名是否添加后缀
        $config['maintain_ratio'] = true;// true：按比例压缩 false 拉伸压缩
        $config['width'] = $size['width'];
        $config['height'] = $size['height'];
        if($newPath){
            $config['new_image'] = $newPath;
        }else{
            $config['new_image'] = $sourcePath;
        }
        $this->CI->image_lib->initialize($config);
        $result = $this->CI->image_lib->resize();
        if (!$result) {
            //提示错误
            echo $this->CI->image_lib->display_errors();
        }
        return $result;
    }

    // 删除服务器上的图片
    public function deleteFile($pathFile)
    {
        $result = unlink ($pathFile);
        if ($result == false) {
            return true;
        } else {
            return false;
        }
    }
    
}
